
package com.thinkgem.jeesite.modules.acq.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.modules.acq.dao.breedinfo.AcqBreedInfoDao;
import com.thinkgem.jeesite.modules.acq.dao.fammer.AcqFarmerDao;
import com.thinkgem.jeesite.modules.acq.dao.plantinfo.AcqPlantInfoDao;
import com.thinkgem.jeesite.modules.acq.entity.breedinfo.AcqBreedInfo;
import com.thinkgem.jeesite.modules.acq.entity.fammer.AcqFarmer;
import com.thinkgem.jeesite.modules.acq.entity.plantinfo.AcqPlantInfo;

/**
 * 分析统计Service
 * 
 * @author lj
 * @version 2018-04-08
 */
@Service
@Transactional(readOnly = true)
public class AcqAnalysisService {
	@Autowired
	AcqBreedInfoDao acqBreedInfoDao;
	@Autowired
	AcqPlantInfoDao acqPlantInfoDao;
	@Autowired
	AcqFarmerDao farmerDao;
	// 生产情况分析
	@Transactional(readOnly = false)
	public Map<String, Object> produceAnalysisList(String areaId) {
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			
			// 查询种植数据
			List<Map<String, Object>> acqPlantInfoList = acqPlantInfoDao.analysisList(areaId);
			List<Map<String, Object>> acqBreedInfoList = acqBreedInfoDao.analysisList(areaId);
			//查询产量与订销量
			Double plantEs = acqPlantInfoDao.es(areaId);
			Double plantDx = acqPlantInfoDao.dx(areaId);
			Double breedEs = acqBreedInfoDao.es(areaId);
			Double breedDx = acqBreedInfoDao.dx(areaId);
			// 查询养殖数据
			result.put("plantEs", plantEs);
			result.put("plantDx", plantDx);
			result.put("breedEs", breedEs);
			result.put("breedDx", breedDx);
			
			result.put("plants", acqPlantInfoList);
			result.put("breeds", acqBreedInfoList);
			result.put("success", true);
			result.put("message", "生产情况统计接口调用成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("message", "生产情况统计接口调用失败");
			return result;
		}

	}

	// 生产情况分析
	@Transactional(readOnly = false)
	public Map<String, Object> produceAnalysis(AcqFarmer acqFarmer) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 1.根据行政区域查询种/养殖集合
			String area = acqFarmer.getArea();
			AcqBreedInfo breedParam = new AcqBreedInfo();
			AcqPlantInfo plantParam = new AcqPlantInfo();
			breedParam.setAcqFarmerId(acqFarmer.getId());
			plantParam.setAcqFarmerId(acqFarmer.getId());
			List<AcqBreedInfo> acqBreedInfos = acqBreedInfoDao.areaBreedList(area);
			List<AcqPlantInfo> acqPlantInfos = acqPlantInfoDao.areaPlantList(area);
			// 2.计算总照片数量
			Integer photocount = countPhoto(acqBreedInfos, acqPlantInfos);
			// 3.统计饼图数据
			List<Map<String, Object>> areaBreedData = acqBreedInfoDao.areaBreedData(area);
			List<Map<String, Object>> acqPlantData = acqPlantInfoDao.areaPlantData(area);
			// 4.图片路径集合
			String[] patharr = new String[] {};
			for (AcqBreedInfo breed : acqBreedInfos) {
				patharr = (String[]) ArrayUtils.addAll(patharr, breed.getPhoto().split(","));
			}
			for (AcqPlantInfo plant : acqPlantInfos) {
				patharr = (String[]) ArrayUtils.addAll(patharr, plant.getPhoto().split(","));
			}
			result.put("photopaths", patharr);
			result.put("photocount", photocount);
			result.put("areaBreedData", areaBreedData);
			result.put("areaPlantData", acqPlantData);
			result.put("success", true);
			result.put("message", "生产情况统计接口调用成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", true);
			result.put("message", "生产情况统计接口调用失败");
			return result;
		}

	}

	public Integer countPhoto(List<AcqBreedInfo> acqBreedInfos, List<AcqPlantInfo> acqPlantInfos) {
		Integer i = 0;
		Integer j = 0;
		for (AcqBreedInfo breed : acqBreedInfos) {
			i = i + breed.getPhoto().split(",").length;
		}
		for (AcqPlantInfo plant : acqPlantInfos) {
			j = j + plant.getPhoto().split(",").length;
		}
		return i + j;
	}

	public Map<String, Object> countTotal(String areaId) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			result.put("totalFamily", farmerDao.totalFamily(areaId));
			result.put("totalPeople", farmerDao.totalPeople(areaId));
			result.put("totalArea", acqPlantInfoDao.totalArea(areaId));
			result.put("totalBreed", acqBreedInfoDao.totalBreed(areaId));
			result.put("success", true);
			return result;
		}catch(Exception e){
			e.printStackTrace();
			result.put("success", false);
			return result;
		}
		
	}

}